package com.kcnet.qk.controller.sjgl;

import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

/**
 * Created by Administrator on 2018/7/11.
 * 群组群发消息
 */

public class SjglSenAllGroupMsgCTR extends BasePeopleController {


    private int mType;
    private String mClassName;
    private static String TAG = "群组群发消息";

    private static SjglSenAllGroupMsgCTR mRongCloudInstance;

    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());

    private String text;

    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static SjglSenAllGroupMsgCTR getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (SjglSenAllGroupMsgCTR.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new SjglSenAllGroupMsgCTR();
                }
            }
        }

        return mRongCloudInstance;
    }


    @Override
    public void doTask(AccessibilityEvent event) throws Exception {
        mType = event.getEventType();
        mClassName = (String) event.getClassName();

        if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.LauncherUI)) {
            NLogger.d(TAG,"1.进入微信首页 点击通讯录按钮");
            sleepTime(2000);
            tagTextClick("通讯录");
            sleepTime(1000);
            NLogger.d(TAG,"2.到达通讯录界面 点击 群聊");
            tagTextClick("群聊");
        }
        else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.ChatroomContactUI)){
            NLogger.d(TAG,"3.到达群列表");
            //获取群聊列表
            beginAddPeople();
        }
        else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.ChattingUI)){
            NLogger.d(TAG, "4 进入聊天界面 发送消息");
            sleepTime(1000);
            RootShell.click(90,900);
            sleepTime(1000);
            inputSayHiSentence(text);
            sleepTime(1000);
            RootShell.click(465,900);
            sleepTime(800);
            RootShell.click(10,38);

        }
    }


    private void beginAddPeople() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            scrllorNodeInfo = NodeUtil.findNodeByClassNameAndTime(root, Const.LIST_VIEW, 0);

            for (int i = 1; i < scrllorNodeInfo.getChildCount(); i++) {

                if(currentAddCount == maxAddCount){
                    currentAddCount = 0;
                    stopTask();
                    return;
                }

                AccessibilityNodeInfo nodeInfo = scrllorNodeInfo.getChild(i);

                if (nodeInfo != null) {
                    List<AccessibilityNodeInfo> nameList = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/aaq");

                    String name = nameList.get(0).getText().toString();

                    if (!mRecordMap.containsKey(name)) {
                        mRecordMap.put(name,true);
                        currentAddCount++;
                        NodeUtil.performClick(nodeInfo);
                        break;

                    }
                    if (i == scrllorNodeInfo.getChildCount() - 1) {
                        NodeUtil.performScroller(scrllorNodeInfo);
                        break;
                    }
                }

            }
        }
    }

    /**
     *
     * @param maxAddCount 群发给群的数量
     * @param minAddTime
     * @param maxAddTime
     * @param text  群发语句
     */
    public void setParms(int maxAddCount, int minAddTime, int maxAddTime, String text) {
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.text = text;
        mRecordMap.clear();

    }

}
